package com.tsingteng.cosfun.ui.login;




import android.util.Log;

import com.tsingteng.cosfun.app.AppContext;
import com.tsingteng.cosfun.bean.ReportBean;
import com.tsingteng.cosfun.bean.UserBean;
import com.tsingteng.cosfun.callback.RxObserver;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.mvp.presenter.BasePresenter;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.utils.ToastUtils;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/8
 * qq(872179966)
 */

public class LogonPresenter extends BasePresenter<LogonContract.ILogonView> implements LogonContract.ILogonPresenter {

    private String username, verifyCode;
    private LogonModel logonModel;
    private LogonContract.ILogonView mLogonView;

    public LogonPresenter() {
        this.logonModel = new LogonModel();
    }




    private void verifyAccount() {
        mLogonView = getView();
        username = mLogonView.getUserName();
        verifyCode = mLogonView.getVerifyCode();

    }

    @Override
    public void login() {
        verifyAccount();
        if (username.length()!=11) {
            mLogonView.showFail("手机号码格式不正确!");
            return;
        }
        if (username.isEmpty()|| verifyCode.isEmpty())  {
            mLogonView.showFail("手机号码或验证码不能为空!");
            return;

        }
        RxObserver<UserBean> rxObserver = new RxObserver<UserBean>(this) {
            @Override
            protected void onFaile(int errorCode, String errorMsg) {
                mLogonView.showFail(errorMsg);
            }

            @Override
            protected void onSuccess(UserBean userBean) {
                mLogonView.showLogonResult(userBean);
                AppUtils.setIsLogin(true);
                //保存用户的信息
                saveUserInfo(userBean);
            }
        };
        logonModel.login(username,verifyCode,String.valueOf(System.currentTimeMillis()),rxObserver);
        addDisposable(rxObserver);
    }

    /**
     * 保存用户信息
     * @param userBean
     */
    private void saveUserInfo(UserBean userBean) {
        LogonUtils.setIsLogin(true);
        LogonUtils.setToken(userBean.getToken());
        LogonUtils.setIsNewUser(userBean.getIs_new_user());
        LogonUtils.setMobile(userBean.getMobile());
        LogonUtils.setProFildId(userBean.getProfile_id());
        LogonUtils.setIsAdmin(userBean.getIs_admin());
        AppContext.init();
        loginReport();

    }

    @Override
    public void verifyCode() {
        verifyAccount();
        if (username.isEmpty())  {
            ToastUtils.showToast("请输入手机号!");
            return;
        }
        RxObserver<UserBean> rxObserver = new RxObserver<UserBean>(this) {
            @Override
            protected void onFaile(int errorCode, String errorMsg) {
                mLogonView.showFail(errorMsg);
            }

            @Override
            protected void onSuccess(UserBean userBean) {
                mLogonView.showVerifyCodeResult(userBean);
            }
        };

        logonModel.verfyCode(username,"6",String.valueOf(System.currentTimeMillis()),rxObserver);
        addDisposable(rxObserver);
    }

    @Override
    public void logonQQ(String open_id, String thirdparty_token, String login_type, String headpic, String nickname) {
        mLogonView= getView();
        RxObserver<UserBean> rxObserver = new RxObserver<UserBean>(this) {
            @Override
            protected void onFaile(int errorCode, String errorMsg) {
                mLogonView.showFail(errorMsg);
            }

            @Override
            protected void onSuccess(UserBean userBean) {
                mLogonView.showQQLogonResult(userBean);
                saveUserInfo(userBean);
                switch (login_type) {
                    case "3":
                        AppUtils.setLoginTag(Config.LOGIN_QQ);
                        break;
                    case "4":
                        AppUtils.setLoginTag(Config.LOGIN_WB);
                        break;
                    case "5":
                        AppUtils.setLoginTag(Config.LOGIN_WX);
                        break;
                }
            }
        };
        logonModel.logonQQ(open_id,thirdparty_token,String.valueOf(System.currentTimeMillis()),login_type,headpic,nickname,rxObserver);
        addDisposable(rxObserver);
    }


    /**
     * 友盟登录上报
     * */
    @Override
    public void loginReport() {
        mLogonView = getView();
        RxObserver<ReportBean> rxObserver = new RxObserver<ReportBean>(this) {
            @Override
            protected void onFaile(int errorCode, String errorMsg) {
                Log.e("TAG666",errorMsg);
            }

            @Override
            protected void onSuccess(ReportBean status) {
                mLogonView.showReportData(status);
            }
        };
        logonModel.getLoginAndReport(rxObserver);
        addDisposable(rxObserver);

    }
}
